package at.fhj.itm10.eventgallery.controller;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.validator.routines.UrlValidator;

import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import at.fhj.itm10.eventgallery.configuration.Configuration;

public class EventJoinerAsync extends AsyncTask<String, String, String>{
	
	private TextView subTitle;
	private ProgressBar progressBar;
	private String eventUrl;
	private static final String PROTOCOL = "http://";
	
	// constructor for manual input option
	public EventJoinerAsync(TextView subTitle, ProgressBar progressBar) {
		this.setSubTitle(subTitle);
		this.setProgressBar(progressBar);
	}

	@Override
	protected String doInBackground(String... params) {
//		this.setEventUrl(params[0]);
		this.setEventUrl("eventgallery.at/");
		if (isURLValid())
			return this.getEventTitle();
		else
			return null;
	}
	
	// defines what happens after doInBackground
	@Override
	protected void onPostExecute(String result) {
		super.onPostExecute(result);
		Log.d("own", "onpostexecute: " + eventUrl);
		if (result != null) {
			subTitle.setText(result);
		} else {
			subTitle.setText("Event not found");
		}
		progressBar.setVisibility(View.INVISIBLE);
	}
	
	// this method checks if the server is reachable
	// and the url is correct
	public boolean isURLValid() {
		UrlValidator validator = new UrlValidator();
		Log.d("own", Configuration.getINSTANCE().getServerUrl() + " " + eventUrl);
		if (validator.isValid(PROTOCOL + eventUrl) && eventUrl.startsWith(Configuration.getINSTANCE().getServerUrl())) {
			return true;
		} else {
			return false;
		}
	}
	
	// searches title in html document
	public String getEventTitle() {
		StringBuffer sb = new StringBuffer();

		try {
			BufferedReader inreader = null;
			URL url = new URL(PROTOCOL + eventUrl);
			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
			if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
				inreader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
				String line;
				while ((line = inreader.readLine()) != null) {
					sb.append(line);
				}
				inreader.close();
			}
			conn = null;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			sb.append(e.getMessage());
		}
		Log.d("own", "geht immer no???");
		
		Pattern p = Pattern.compile("<title>(.*?)</title>");
		Matcher m = p.matcher(sb.toString());
	    
		String title = null;
		while (m.find() == true) {
			Log.d("own", m.group(1));
			title = m.group(1);
		}
		
		return title;
	}
	
	// getter and setter
	public TextView getSubTitle() {
		return subTitle;
	}
	public void setSubTitle(TextView subTitle) {
		this.subTitle = subTitle;
	}
	public ProgressBar getProgressBar() {
		return progressBar;
	}
	public void setProgressBar(ProgressBar progressBar) {
		this.progressBar = progressBar;
	}
	public String getEventUrl() {
		return eventUrl;
	}
	public void setEventUrl(String eventUrl) {
		// some versions of zxing put a "http://" in front of the url
		if (eventUrl.startsWith("http://")) {
			eventUrl = eventUrl.split("http://")[1];
		}
		this.eventUrl = eventUrl;
	}
}
